type Topic = number


const NO_HELP_TOPIC: Topic = -1

class HelpHandler {
    declare private _successor: HelpHandler | 0
    declare private _topic: Topic

    constructor(successor: HelpHandler | 0 = 0, topic: Topic = NO_HELP_TOPIC) {
        this._topic = topic
        this._successor = successor
    }
    hasHelp(): boolean {
        return this._topic != NO_HELP_TOPIC
    }
    setHandler(successor: HelpHandler | 0, topic: Topic) {
        this._topic = topic
        this._successor = successor
    }
    handleHelp() {
        if (this._successor != 0) {
            this._successor.handleHelp()
        }
    }
}
class Widget extends HelpHandler {
    declare private _parent: Widget | 0
    constructor(parent: Widget | 0, topic: Topic = NO_HELP_TOPIC) {
        super(parent, topic)
        this._parent = parent
    }
}
class Button extends Widget {
    constructor(parent: Widget | 0, topic: Topic = NO_HELP_TOPIC) {
        super(parent, topic)
    }
    handleHelp(): void {
        if (this.hasHelp()) {
            console.log('在按钮上提供帮助')
        } else {
            super.handleHelp()
        }
    }
}
class Dialog extends Widget {
    constructor(successor: HelpHandler | 0 = 0, topic: Topic = NO_HELP_TOPIC) {
        super(0)
        this.setHandler(successor, topic)
    }
    handleHelp(): void {
        if (this.hasHelp()) {
            console.log('在对话框上提供帮助')
        } else {
            super.handleHelp()
        }
    }

}
class Application extends HelpHandler {
    constructor(t: Topic) {
        super(0, t)
    }
    handleHelp(): void {
        console.log('帮助主题列表');
    }
}


const PRINT_TOPIC: Topic = 1
const PAPER_ORIENTATION_TOPIC: Topic = 2
const APPLICATION_TOPIC: Topic = 3


const application = new Application(APPLICATION_TOPIC)
const dialog = new Dialog(application, PRINT_TOPIC)
const button = new Button(dialog, PAPER_ORIENTATION_TOPIC)

button.handleHelp()